package com.lagou.rocketmq.listener;


import com.lagou.rocketmq.mapper.GoodsMapper;
import com.lagou.rocketmq.mapper.OrderMapper;
import com.lagou.rocketmq.pojo.Order;
import com.lagou.rocketmq.pojo.OrderExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import redis.clients.jedis.JedisPool;

import java.util.Date;
import java.util.List;

@Configuration
@EnableScheduling
public class OrderProcess {


    @Autowired
    GoodsMapper goodsMapper;

    @Autowired
    OrderMapper orderMapper;

    @Autowired
    JedisPool jedisPool;

    @Scheduled(fixedRate = 5000)
    public void orderProcess() {

        OrderExample orderExample = new OrderExample();
        orderExample.createCriteria().andStatusEqualTo(0);
        List<Order> orders = orderMapper.selectByExample(orderExample);

        if(orders != null && orders.size()>0){
            for (Order order : orders) {
                Date createDate = order.getCreateDate();
                Date date = new Date();
                if((date.getTime() - createDate.getTime()) > 30 * 60 * 1000 && order.getStatus() == 0){
                    jedisPool.getResource().incrBy("total",1);
                    goodsMapper.updateGoodsCount(null,order.getGoodsid());
                    orderMapper.updateOrderStatus(order.getOrderid(),1);
                }
            }
        }
    }

}